package mskim.study.ooad.ch01.fifth;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Inventory {
	private List<Guitar> guitars;

	public Inventory() {
		guitars = new ArrayList<Guitar>();
	}
	
	public void addGuitar(String serialNumber, double price, Builder builder,
			String model, Type type, Wood backWood, Wood topWood){
		//Guitar guitar= new Guitar(serialNumber, price, builder, model, type, backWood, topWood);
		Guitar guitar= new Guitar(serialNumber, price, new GuitarSpec(builder, model, type, backWood, topWood));
		guitars.add(guitar);
	}
	
	public Guitar getGuitar(String serialNumber){
		for(Iterator<Guitar> i=guitars.iterator(); i.hasNext(); ){
			Guitar guitar = i.next();
			if (guitar.getSerialNumber().equals(serialNumber)){
				return guitar;
			}
		}
		return null;
	}
	
	public List<Guitar> search(GuitarSpec searchSpec){
		List<Guitar> matchingGuitars = new ArrayList<Guitar>();
		for (Iterator<Guitar> i = guitars.iterator(); i.hasNext(); ) {
			Guitar guitar = (Guitar)i.next();
			GuitarSpec spec = guitar.getSpec();
		      // Ignore serial number since that's unique
		      // Ignore price since that's unique

			if (searchSpec.getBuilder() != spec.getBuilder()) continue;
		      
			String model = searchSpec.getModel().toLowerCase();
			if ((model != null) && (!model.equals("")) && (!model.equals(spec.getModel().toLowerCase()))) continue;

			if (searchSpec.getType() != spec.getType()) continue;
			if (searchSpec.getBackWood() != spec.getBackWood()) continue;
			if (searchSpec.getTopWood() != spec.getTopWood()) continue;
			matchingGuitars.add(guitar);
		}
		return matchingGuitars;
	}
}
